package cn.wolfcode.ssm.service.impl;

import cn.wolfcode.ssm.domain.Role;
import cn.wolfcode.ssm.mapper.RoleMapper;
import cn.wolfcode.ssm.qo.QueryObject;

import cn.wolfcode.ssm.service.IRoleService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Transactional
@Service
public class RoleServiceImpl implements IRoleService {


    @Autowired
    private RoleMapper roleMapper;


    @Override
    public void deleteById(Long id) {
        if(id != null){
            roleMapper.deleteByPrimaryKey(id);
        }
    }

    @Override
    public void save(Role role, Long[] ids) {
        roleMapper.insert(role);
        roleMapper.insertRelation(role.getId(),ids);

    }

    @Override
    public Role selectById(Long id) {
        if(id != null){
            return roleMapper.selectByPrimaryKey(id);
        }
        return null;
    }



    @Override
    public void updateById(Role role, Long[] ids) {
        roleMapper.deleteRelation(role.getId());
        roleMapper.updateByPrimaryKey(role);
        roleMapper.insertRelation(role.getId(),ids);
    }

    @Override
    public List<Role> listAll() {
        return roleMapper.selectAll();
    }


    @Override
    public PageInfo<Role> query(QueryObject qo) {
        //开始分页
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize());
        //拦截对应的sql，加上LIMIT字句
        List<Role> roles = roleMapper.selectAll();
        //自动封装（只需要提供List参数）
        return new PageInfo<>(roles);


    }


}
